#!/bin/sh
# Copyright 2013,2017 Darren Bane. All rights reserved.
# Use of this source code is governed by the
# license that can be found in the NOTICE file.

# git indent pre-commit hook
#
# To use, symlink to .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
#
# This script does not handle file names that contain spaces.

cppfiles=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|h)$')
[ -z "$cppfiles" ] && exit 0

unformatted=""
for fn in $cppfiles; do
	astyle < $fn > /tmp/indent.$$
	if ! diff $fn /tmp/indent.$$ > /dev/null; then
		unformatted="$unformatted $fn";
	fi
done
rm /tmp/indent.$$
[ -z "$unformatted" ] && exit 0

# Some files are not indented. Print message and fail.

echo >&2 "C++ files must be formatted with astyle. Please run:"
for fn in $unformatted; do
	echo >&2 "  astyle $PWD/$fn"
done

exit 1
